﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Altairis.RssFrame.Updater {
    class Program {

        private const int ExitCodeSuccess = 0;
        private const int ExitCodeFailure = 1;

        static void Main(string[] args) {
            Console.WriteLine("RSS Feed updater | (c) Altairis, 2008 | www.altairis.cz");

            // Open master XML document
            Console.Write("Opening data file {0}...", Properties.Settings.Default.DataFileName);
            XmlDocument mainDoc = new XmlDocument();
            try {
                mainDoc.Load(Properties.Settings.Default.DataFileName);
                Console.WriteLine("OK");
            }
            catch (Exception ex) {
                Console.WriteLine("Failed!");
                Console.WriteLine(ex.Message);
                Environment.Exit(ExitCodeFailure);
            }

            // Process all feeds
            foreach (XmlElement feed in mainDoc.SelectNodes("/root/group/feed")) {
                // Download feed
                XmlDocument feedDoc = new XmlDocument();
                Console.Write("Downloading feed '{0}' from {1}...", feed.GetAttribute("id"), feed.GetAttribute("url"));
                feed.SelectSingleNode("last/@time").Value = XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.RoundtripKind);
                try {
                    feedDoc.Load(feed.GetAttribute("url"));
                    Console.WriteLine("OK");
                }
                catch (Exception ex) {
                    Console.WriteLine("Failed!");
                    Console.WriteLine(ex.Message);
                    feed.SelectSingleNode("last/@status").Value = ex.Message;
                    continue;
                }

                // Check for channel items
                Console.Write("Importing nodes...");
                XmlElement channel = feedDoc.SelectSingleNode("/rss/channel") as XmlElement;
                if (channel == null || channel.ChildNodes.Count == 0) {
                    Console.WriteLine("Failed!");
                    Console.WriteLine("Invalid feed format. The /rss/channel element is missing or empty.");
                    feed.SelectSingleNode("last/@status").Value = "Invalid feed format. The /rss/channel element is missing or empty.";
                    continue;
                }

                // Remove all current channel items
                XmlElement dataNode = feed.SelectSingleNode("data") as XmlElement;
                dataNode.RemoveAll();

                // Add new channel items
                foreach (XmlNode item in channel.ChildNodes) {
                    dataNode.AppendChild(dataNode.OwnerDocument.ImportNode(item, true));
                }
                feed.SelectSingleNode("last/@status").Value = "OK";
                Console.WriteLine("OK, {0} nodes imported.", channel.ChildNodes.Count);
            }

            // Save changes
            try {
                Console.Write("Saving changes...");
                mainDoc.Save(Properties.Settings.Default.DataFileName);
                Console.WriteLine("OK");
            }
            catch (Exception ex) {
                Console.WriteLine("Failed!");
                Console.WriteLine(ex.Message);
                Environment.Exit(ExitCodeFailure);
            }

            Environment.Exit(ExitCodeSuccess);
        }

    }
}
